package com.wbao.dianniu.viewpager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;

public class MyViewPager extends FragmentActivity {
    public static final String EXTRA_IMAGE_URLS = "intent_extra_image_urls";
    public static final String EXTRA_IMAGE_INDEX = "intent_extra_image_index";
    private ImageView[] tips;// 提示性点点数组
    private ArrayList<String> url_list;
    private int currentPage = 0;// 当前展示的页码

    ImageViewPagerAdapter adapter;
    HackyViewPager pager;

    @Override
    protected void onCreate(@Nullable Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_my_viewpager);
        pager = findViewById(R.id.hacky_view_pager);
        // 存放点点的容器
        LinearLayout tipsBox = findViewById(R.id.tipsBox);
        Intent intent = getIntent();
        url_list = intent.getStringArrayListExtra(EXTRA_IMAGE_URLS);
        currentPage = intent.getIntExtra(EXTRA_IMAGE_INDEX, 0);
        // 初始化 提示点点
        tips = new ImageView[url_list.size()];
        for (int i = 0; i < tips.length; i++) {
            ImageView img = new ImageView(this);
            img.setLayoutParams(new LayoutParams(Utils.dip2px(this, 5), Utils.dip2px(this, 5)));
            tips[i] = img;
            if (i == currentPage) {
                img.setBackgroundResource(R.drawable.ic_focus_);
            } else {
                img.setBackgroundResource(R.drawable.ic_normal);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
            params.leftMargin = 10;
            params.rightMargin = 10;
            tipsBox.addView(img, params);
        }

        adapter = new ImageViewPagerAdapter(getSupportFragmentManager(), url_list);
        pager.setAdapter(adapter);
        pager.setCurrentItem(currentPage);
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                tips[currentPage].setBackgroundResource(R.drawable.ic_normal);
                currentPage = position;
                tips[position].setBackgroundResource(R.drawable.ic_focus_);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

}
